const express = require('express');
const r = express.Router();
const app = express();
const {randomCode,sendCode}=require("./utils/getMessage");
const pool = require('../pool');

// 用户注册接口
// http://127.0.0.1:8888/v1/user/reg
r.post('/reg', (req, res, next) => {
  // 接收用户注册信息
  let obj = req.body;
  console.log(obj);
  pool.query('select * from hqyc_user where phone=?', [obj.phone], (err, result) => {
    if (err) {
      next(err)
      return;
    };
    if (result) {
      if(obj.password!==''&&obj.password!==null){
        if(obj.email!==''&&obj.email!==null){
          // 代表是用邮箱加密码登录
          pool.query('select * from hqyc_user where email=?',[obj.email],(err,result)=>{
            if(err){
              next(err)
              return;
            };
            //判断是否有数据  && 密码和输入的密码相同
            if(result[0]!=null && result[0].upwd == obj.password){
              res.send({
                code: 5,
                msg: "登录成功",
                data: result
              });
            }
          })
        }else if(obj.phone !=='' && obj.phone !== null){
          //代表是用手机号加密码登录的
          pool.query('select * from hqyc_user where phone=?',[obj.phone],(err,result)=>{
            if(err){
              next(err)
              return;
            };
            if(result[0]!=null && result[0].upwd == obj.password){
              console.log(result[0].upwd)
              res.send({
                code: 2,
                msg: "登录成功",
                data: result
              });
            }
          })
        }
      }else{
        res.send({
          code: 3,
          msg: "该手机号已被注册"
        });
      }
    } else {
      pool.query("insert into hqyc_user set ? ", [obj], (err, result) => {
        if (err) {
          next(err)
          return;
        };
        console.log(result);
        result.affectedRows == 1 ? res.send({
          code: 1,
          msg: '用户注册成功',
          data: result
        }) : res.send({
          code: 0,
          msg: '用户注册失败',
          data: result
        });
      })
    }
  })

})
r.get("/login",(req,res)=>{
  let code=randomCode(6);//生成6位数字随机验证码
  sendCode(req.query.phone,code,function(success){
    
    console.log(req.query.phone);
    console.log(success);
      if(success){
          res.send({code:200,msg:"短信验证码已发送",data:code});
      }else{
          res.send({code:201,msg:"短信验证码发送失败"});
      }
  })
})

// 查询用户信息
// http://127.0.0.1:8888/v1/user/userinfo
r.post('/userinfo', (req, res, next) => {
  // 接收用户id
  let obj = req.body;
  console.log(obj);
  pool.query("select * from hqyc_user where uid=?", [obj.uid], (err, result) => {
    if (err) {
      next(err)
      return;
    };
    console.log(result);
    result.length == 1 ? res.send({
      code: 1,
      msg: '用户信息查询成功',
      data: result
    }) : res.send({
      code: 0,
      msg: '用户信息查询失败',
      data: result
    });
  })
})

// 修改用户信息
// http://127.0.0.1:8888/v1/user/upinfo
r.post('/upinfo', (req, res, next) => {
  // 接收用户id
  let obj = req.body;
  console.log(obj);
  pool.query("update hqyc_user set ? where uid=?", [obj, obj.uid], (err, result) => {
    if (err) {
      next(err)
      return;
    };
    console.log(result);
    result.affectedRows == 1 ? res.send({
      code: 1,
      msg: '用户信息修改成功',
      data: result
    }) : res.send({
      code: 0,
      msg: '用户信息修改失败',
      data: result
    });
  })
})

// 删除用户
// http://127.0.0.1:8888/v1/user/del
r.post('/del', (req, res, next) => {
  // 接收用户id
  let obj = req.body;
  console.log(obj);
  pool.query("delete from hqyc_user where uid=?", [obj.uid], (err, result) => {
    if (err) {
      next(err)
      return;
    };
    console.log(result);
    result.affectedRows == 1 ? res.send({
      code: 1,
      msg: '用户删除成功',
      data: result
    }) : res.send({
      code: 0,
      msg: '用户删除失败',
      data: result
    });
  })
})

//国际区号
// http://127.0.0.1:8888/v1/user/areacode
r.get('/areacode',(req,res,next)=>{
  // let obj = req.query;
  // console.log(obj);
  pool.query('select area from country_mobile_prefix group by area',(err,result)=>{
    if(err){
      next(err);
      return;
    }
    res.send({
      code:200,
      msg:'ok',
      data:result
    })
  })
})

//根据area字段获取数据
// http://127.0.0.1:8888/v1/user/selectareacodebyName
r.get('/selectareacodebyName',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from country_mobile_prefix where area=?',[obj.name],(err,result)=>{
    if(err){
      next(err);
      return;
    }
    res.send({
      code:200,
      msg:'ok',
      data:result
    })
  })
})

module.exports = r;